package com.huluobo.a2107mvpimdemo;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.huluobo.a2107mvpimdemo.chat.ChatActivity;
import com.hyphenate.EMMessageListener;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.modules.conversation.EaseConversationListFragment;

import java.util.List;

public class ConversationFragment extends EaseConversationListFragment {

    @Override
    public void initListener() {
        super.initListener();
        EMClient.getInstance().chatManager().addMessageListener(new EMMessageListener() {
            @Override
            public void onMessageReceived(List<EMMessage> messages) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        conversationListLayout.getListAdapter().notifyDataSetChanged();
                    }
                });
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        conversationListLayout.getListAdapter().notifyDataSetChanged();
    }

    @Override
    public void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        //通过会话列表获取会话id,如果是用户聊天,会话id就是对方的username,如果是群聊天,会话id就是groupId
        String conversationId = ((EMConversation) conversationListLayout.getItem(position).getInfo()).conversationId();
        int chatType = 0;
        switch (((EMConversation) conversationListLayout.getItem(position).getInfo()).getType()) {
            case Chat:
                chatType = 1;
                break;
            case GroupChat:
                chatType = 2;
                break;
        }

        Intent intent = new Intent(getActivity(), ChatActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("conversationId", conversationId);
        bundle.putInt("chatType", chatType);
        intent.putExtras(bundle);
        getActivity().startActivity(intent);
    }
}